package com.sky.wsp.solr.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.wsp.solr.service.SolrService;
import com.sky.wsp.solr.vo.ProjectItemDto;
import com.sky.wsp.solr.vo.ProjectItemEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.*;
import org.springframework.data.solr.core.query.result.HighlightEntry;
import org.springframework.data.solr.core.query.result.HighlightPage;
import org.springframework.data.solr.core.query.result.ScoredPage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

//@Service("solrService")
public class SolrTemplateServiceImpl implements SolrService {

    @Resource
    private SolrTemplate solrTemplate;

    // solr 中定义的索引库名称
    private final String INDEX_LIBRARY_NAME="devops_core";

    @Override
    public void update(ProjectItemEntity projectItemEntity) {

        solrTemplate.saveBean(INDEX_LIBRARY_NAME, projectItemEntity);
        solrTemplate.commit(INDEX_LIBRARY_NAME);
    }

    @Override
    public void updateBatch(List<ProjectItemEntity> projectItemEntities) {
        solrTemplate.saveBeans(INDEX_LIBRARY_NAME, projectItemEntities);
        solrTemplate.commit(INDEX_LIBRARY_NAME);
    }

    @Override
    public void delete(List<String> ids) {
        solrTemplate.deleteByIds(INDEX_LIBRARY_NAME, ids);
        solrTemplate.commit(INDEX_LIBRARY_NAME);
    }

    @Override
    public IPage<ProjectItemEntity> queryList(Integer pageIndex, Integer pageSize, String itemKey) {
        // 通用条件模板
        Criteria criteria = Criteria.where("itemKey").is(itemKey);
        // 查询条件对象
        Query query = Query.query(criteria);
        // 通过主键倒序排序
        query.addSort(Sort.by(Sort.Direction.DESC, "id"));
        // 分页(页面从 0 开始)
        query.setPageRequest(PageRequest.of(pageIndex-1, pageSize));
        // query.setPageRequest(PageRequest.of(pageIndex, pageSize, Sort.by(Sort.Direction.DESC, "id")));
        ScoredPage<ProjectItemEntity> pageList = solrTemplate.queryForPage(INDEX_LIBRARY_NAME, query, ProjectItemEntity.class);

        List<ProjectItemEntity> result = pageList.getContent(); // 分页数据
        long totalElements = pageList.getTotalElements(); // 总条数

        IPage<ProjectItemEntity> page = new Page<>(pageIndex, pageSize);
        page.setTotal(totalElements);
        page.setRecords(result);
        return page;
    }

    @Override
    public IPage<ProjectItemDto> queryHighlightList(Integer pageIndex, Integer pageSize, String itemKey) {
        HighlightQuery query = new SimpleHighlightQuery();
        query.addCriteria(Criteria.where("itemKey").is(itemKey));
        query.setPageRequest(PageRequest.of(pageIndex-1, pageSize, Sort.by(Sort.Direction.DESC, "id")));

        HighlightOptions options = new HighlightOptions();
        options.addField("itemKey");
        options.setSimplePrefix("<span style='color:red'>");
        options.setSimplePostfix("</span>");

        query.setHighlightOptions(options);
        HighlightPage<ProjectItemEntity> pageList =
                solrTemplate.queryForHighlightPage(INDEX_LIBRARY_NAME, query, ProjectItemEntity.class);

        // 分页数据，不包括高亮
        List<ProjectItemEntity> result = pageList.getContent();
        List<ProjectItemDto> dtoList = Optional.ofNullable(result).orElse(new ArrayList<>()).stream().map(item->{
            ProjectItemDto dto = new ProjectItemDto();
            BeanUtils.copyProperties(item, dto);
            // 根据当前 POJO 对象，查询高亮数据
            List<HighlightEntry.Highlight> highlights = pageList.getHighlights(item);
            dto.setHighlights(highlights);
            return dto;
        }).collect(Collectors.toList());

        long totalElements = pageList.getTotalElements(); // 总条数

        IPage<ProjectItemDto> page = new Page<>(pageIndex, pageSize);
        page.setTotal(totalElements);
        page.setRecords(dtoList);
        return page;
    }
}
